/**
 * Created by sun on 2015/3/12.
 */
package com.jbm.jbmsupplier.util;
import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolEncoderAdapter;
import org.apache.mina.filter.codec.ProtocolEncoderOutput;
import org.slf4j.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

import java.nio.charset.Charset;

public class OHEncoder extends ProtocolEncoderAdapter
{
    private final String charset;
    public OHEncoder(String charset) {
        this.charset = charset;
    }

    public void encode(IoSession session, Object message,
                       ProtocolEncoderOutput out) throws Exception {

        IoBuffer buf = IoBuffer.allocate(100).setAutoExpand(true);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(message);
        byte[] bytes = baos.toByteArray();

        buf.putInt(bytes.length);
        buf.put(bytes);
        buf.flip();
        out.write(buf);
    }
}
